package com.yawn;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;

import java.util.List;
import java.util.UUID;

/**
 * 流程定义的权限
 * @author Created by yawn on 2017-12-10 10:04
 */
public class TestProcessDefineAuth {

    public static void main(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        IdentityService identityService = processEngine.getIdentityService();
        RepositoryService repositoryService = processEngine.getRepositoryService();

        // 创建一个用户
        User user = identityService.newUser(UUID.randomUUID().toString());
        user.setEmail("com.yawn@qq.com");
        user.setFirstName("王");
        user.setLastName("yawn");
        user.setPassword("123456");
        identityService.saveUser(user);

        // 发布一个流程
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("demo4.bpmn").deploy();

        // 查询流程定义
        ProcessDefinition processDefinition = repositoryService
                .createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();

        // 给流程定义设置用户权限
        repositoryService.addCandidateStarterUser(processDefinition.getId(), user.getId());
        repositoryService.addCandidateStarterGroup(processDefinition.getId(), "1000111");

        // 查询某用户有权限的流程定义
        List<ProcessDefinition> definitionList = repositoryService
                .createProcessDefinitionQuery()
                .startableByUser(user.getId())
                .list();
        definitionList.forEach(System.out::println);

        processEngine.close();
    }
}
